<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Project: Vietsam
 * Package Controller
 * Class: Tab
 * Author: Khoi Pham		email: pmkhoi@gmail.com
 * Created on Dec 18, 2008
 * Updated on Dec 30, 2008
 *
 * */
class Permission extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('tab_model', 'tab', true);
        $this->load->model('profile_model', 'profile', true);
        $this->load->model('access_model', 'access', true);

        $this->checkLogin();
        $this->checkPermission();
        
        $this->template->set_template('admin');
        $this->buildMenu();
        $this->template->add_js('js/jquery1.6.1.js');
    }

    function Permission() {
        parent::VNT_Controller();
        $this->load->library('xajax', 'permission');
        $this->xajax->registerFunction(array('change_access', &$this, 'change_access'));

        $this->xajax->processRequest();
        $this->xajax_js = $this->xajax->getJavascript($this->base_url . 'js/');
        $this->load->model('tab_model', 'tab', true);
        $this->load->model('profile_model', 'profile', true);
        $this->load->model('access_model', 'access', true);
        $this->main = '';
        $this->checkLogin();
        $this->checkPermission();
    }

    function index() {
        $this->view();
    }

    function view($id_default_profile = 0) {
        $NUMBER_RECORD_PER_PAGE = 100;
        $this->load->library('pagination');
        $page = (int) $this->uri->segment(5);
        $config['base_url'] = $this->base_url . 'admin/permission/view/';
        $config['total_rows'] = $this->tab->getAllCount(-1);
        $config['per_page'] = $NUMBER_RECORD_PER_PAGE;
        $config['uri_segment'] = 4;
        $config['first_link'] = 'First';

        $this->pagination->initialize($config);

        $rows = $this->tab->getAll(-1, $page, $NUMBER_RECORD_PER_PAGE);
        $tmp = array();
        for ($i = 0; $i < count($rows); $i++) {
            $tmp[$rows[$i]->id_parent_tab][$rows[$i]->id_tab] = $rows[$i];
        }

        $profiles = $this->profile->getAll();

        //$id_default_profile = 0;
        if (is_array($profiles) AND $id_default_profile == 0) {
            $id_default_profile = $profiles[0]['id_profile'];
        }

        $tmp_access = $this->access->getByProfile($id_default_profile);
        $access = array();
        for ($i = 0; $i < count($tmp_access); $i++) {
            $access[$tmp_access[$i]->id_tab] = $tmp_access[$i];
        }

        //print_r($access);
        $data = array();
        $data['rows'] = $rows;
        $data['tmp'] = $tmp;

        $data['profiles'] = $this->buidDDL($profiles, 'id_profile', $id_default_profile, 'onchange="change_profile();"');
        $data['access'] = $access;
        //print_r($access);

        $this->template->parse_view('content', 'admin/permission/permission_list', $data);
        $this->template->render();

    }

    function change_access($action, $id_profile, $id_tab, $access) {
        $access = $access == true ? 1 : 0;
         $this->load->model('access_model', 'access', true);
        $row = $this->access->getById($id_profile, $id_tab);

        $info = array();

        if ($row) {
            if ($action == 'view')
                $row->view = $access;
            elseif ($action == 'add')
                $row->add = $access;
            elseif ($action == 'edit')
                $row->edit = $access;
            elseif ($action == 'delete')
                $row->delete = $access;

            $result = $this->access->update($row);
            if($result > 0){
                $info['error'] = FALSE;
                $info['msg'] = "Update profile sucessful." . $result;
            }
            else{
                $info['error'] = TRUE;
                $info['msg'] = "Update profile not sucessful." . $result;
            }
        }
        else{
            $info['error'] = TRUE;
            $info['msg'] = "Not found profile.";
        }
       
        echo json_encode($info);
    }

    function buidDDL($items, $name, $default_value, $action = '', $style = '', $class = '') {
        $options = '';
        $space = '';
        for ($i = 0; $i < count($items); $i++) {
            $label = $items[$i]['vi_name'];
            $value = $items[$i]['id_profile'];
            if ($value == $default_value)
                $selected = 'selected';
            else
                $selected = '';
            //$options .= eval($this->loadTemplate('admin/option_ddl.tpl'));
            $options .= $this->load->view('admin/option_select', array('value' => $value, 'selected' => $selected, 'space' => $space, 'label' => $label), true);
        }

       // $data['options'] = $options;
        $data['name'] = $name;
        $data['action'] = $action;
        $data['style'] = $style;
        $data['class'] = $class;
        $data['options'] = $options;
        //return eval($this->loadTemplate('admin/ddl.tpl'));
        return $this->load->view('admin/select', $data, true);;
    }

}

?>
